package demo1;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("lisi", 18);
        Student student2 = new Student("gaobo", 8);
        Student student3 = new Student("weipeng", 33);
        Student[] students = new Student[3];
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;

        System.out.println(Arrays.toString(students));
        AgeComparator ageComparator = new AgeComparator();
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students,nameComparator);
        //bubbleSort(students);
        //Arrays.sort(students,ageComparator);
        //Arrays.sort(students);//Arrays.sort底层也是用到了compareTo这个方法

        System.out.println(Arrays.toString(students));
    }


    public static void main3(String[] args) {
        Student student1 = new Student("aisi", 18);
        Student student2 = new Student("baobo", 8);
        NameComparator nameComparator = new NameComparator();
        int name = nameComparator.compare(student1, student2);
        System.out.println("根据姓名比较");
        //int ret = student1.compareTo(student2);
        if (name > 0) {
            System.out.println("student1>student2");
        } else if (name == 0) {
            System.out.println("student1==student2");
        } else {
            System.out.println("student1<student2");
        }
        System.out.println("根据年龄比较");
        AgeComparator ageComparator = new AgeComparator();
        int age = ageComparator.compare(student1, student2);
        //int ret = student1.compareTo(student2);
        if (age > 0) {
            System.out.println("student1>student2");
        } else if (age == 0) {
            System.out.println("student1==student2");
        } else {
            System.out.println("student1<student2");
        }

    }


    public static void bubbleSort(Comparable[] comparable) {
        for (int i = 0; i < comparable.length - 1; i++) {
            for (int j = 0; j < comparable.length - 1 - i; j++) {
                //if (comparable[j]>comparable[j+1])
                if (comparable[j].compareTo(comparable[j + 1]) > 0) {
                    Comparable tmp = comparable[j];
                    comparable[j] = comparable[j + 1];
                    comparable[j + 1] = tmp;
                }
            }
        }


    }


    public static void main2(String[] args) {
        Student student1 = new Student("lisi", 18);
        Student student2 = new Student("gaobo", 8);
        Student student3 = new Student("weipeng", 33);
        Student[] students = new Student[3];
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;

        System.out.println(Arrays.toString(students));

        //bubbleSort(students);
        Arrays.sort(students);
        //Arrays.sort(students);//Arrays.sort底层也是用到了compareTo这个方法

        System.out.println(Arrays.toString(students));
    }

    public static void main1(String[] args) {
        Student student1 = new Student("lisi", 18);
        Student student2 = new Student("gaobo", 8);

        int ret = student1.compareTo(student2);
        if (ret > 0) {
            System.out.println("student1>student2");
        } else if (ret == 0) {
            System.out.println("student1==student2");
        } else {
            System.out.println("student1<student2");
        }
    }
}
